/**
 * Copyright &copy; 2012-2016 civet All rights reserved.
 */
package cn.mcsys.myadmin.modules.air.web;

import cn.mcsys.myadmin.common.config.Global;
import cn.mcsys.myadmin.common.persistence.Page;
import cn.mcsys.myadmin.common.web.BaseController;
import cn.mcsys.myadmin.common.web.JsonResult;
import cn.mcsys.myadmin.modules.air.entity.AirSendMessage;
import cn.mcsys.myadmin.modules.air.entity.AirWorker;
import cn.mcsys.myadmin.modules.air.service.AirSendMessageService;
import cn.mcsys.myadmin.modules.air.service.AirWorkerService;
import cn.mcsys.myadmin.modules.sendMessage.APIClient;
import cn.mcsys.myadmin.modules.viewer.service.AirRideQRCodeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;

/**
 * 发布消息Controller
 * @author leopold
 * @version 2018-08-13
 */
@Controller
@RequestMapping(value = "${adminPath}/air/airSendMessage")
public class AirSendMessageController extends BaseController {

	@Autowired
	private AirSendMessageService airSendMessageService;

	@Autowired
	private AirRideQRCodeService airRideQRCodeService;

	@Autowired
	private AirWorkerService airWorkerService;

	@RequiresPermissions("air:airSendMessage:view")
	@RequestMapping(value = {"list", ""})
	public String list(AirSendMessage airSendMessage, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<AirSendMessage> page = airSendMessageService.findPage(new Page<AirSendMessage>(request, response), airSendMessage);
		model.addAttribute("page", page);
		return "modules/air/airSendMessageList";
	}

	@RequiresPermissions("air:airSendMessage:view")
	@RequestMapping(value = "form")
	public String form(AirSendMessage airSendMessage, Model model) {
	    if (airSendMessage.getId() != null){
            airSendMessage = airSendMessageService.get(airSendMessage.getId());
        }
		model.addAttribute("airSendMessage", airSendMessage);
		return "modules/air/airSendMessageForm";
	}

	@RequiresPermissions("air:airSendMessage:edit")
	@RequestMapping(value = "save")
	public String save(AirSendMessage airSendMessage, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request) {
		if (!beanValidator(model, airSendMessage)){
			return form(airSendMessage, model);
		}
		airSendMessage.setSendType(1);
		String sendToPhone = request.getParameter("sendToPhone");//要么null 要么 1
		if("1".equalsIgnoreCase(sendToPhone)){
			airSendMessage.setSendType(2);
			//手机短信接口   根据 receiver 发送信息给相应的所有接收方的手机
            APIClient apiClient = new APIClient();
            apiClient.init("192.168.133.46:3306","api123","WWww321321","api123");
			if (airSendMessage.getRecevier() == AirSendMessage.viewer){
//				查询二维码表的所有，但是如果一次性读取太多就回卡
				int pageNo = 0;
				while (true){
					List<String> phoneList = airRideQRCodeService.selectByPage(pageNo);
					if (phoneList == null || phoneList.size() == 0){
						break;
					}
					String[] stringPhones = new String[phoneList.size()];
					String[] phones = phoneList.toArray(stringPhones);
//					每一千条发一次
					apiClient.sendSM(phones,airSendMessage.getContent(),10L);
					pageNo+=10000;
				}

			}else if (airSendMessage.getRecevier() == AirSendMessage.worker) {
				List<String> phoneList = airWorkerService.findAllPhone();
                String[] stringPhones = new String[phoneList.size()];
				stringPhones = phoneList.toArray(stringPhones);
                apiClient.sendSM(stringPhones,airSendMessage.getContent(),10L);
			}
		}
		airSendMessageService.save(airSendMessage);
//		addMessage(redirectAttributes, "保存消息成功");
		return "redirect:"+Global.getAdminPath()+"/air/airSendMessage/?repage";
	}
	
	@RequiresPermissions("air:airSendMessage:edit")
	@RequestMapping(value = "delete")
	public String delete(AirSendMessage airSendMessage, RedirectAttributes redirectAttributes) {
		airSendMessageService.delete(airSendMessage);
//		addMessage(redirectAttributes, "删除消息成功");
		return "redirect:"+Global.getAdminPath()+"/air/airSendMessage/?repage";
	}

	/**
	 * 获取通知、公告信息
	 * @param type 接收者的类型：1 代表观众，2 代表工作人员
	 * @return
	 */
	@GetMapping("getMessageByReceiverType")
	@ResponseBody
	public JsonResult getMessageByReceiverType(int type){
		return JsonResult.success(airSendMessageService.getMessageByReceiverType(type));
	}

}